package simple; /**
 * @projectName study-note
 * @package PACKAGE_NAME
 * @className PACKAGE_NAME.MergeArr
 */

import util.GeneratorRandomArr;

/**
 * MergeArr
 * @description 合并数组
 * @author answer_wx
 * @date 2022/7/5 22:32
 * @version 1.0
 */
public class MergeArr {
    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int i = 0;
        int j = 0;
        int[] re = new int[m + n];
        int index = 0;
        while (i < m || j < n) {
            if (i > m - 1) {
                re[index++] = nums2[j++];
            } else if (j > n - 1) {
                re[index++] = nums1[i++];
            } else {
                if (nums1[i] < nums2[j]) {
                    re[index++] = nums1[i++];
                } else {
                    re[index++] = nums2[j++];
                }
            }
        }
        for (int k = 0; k < re.length; k++) {
            nums1[k] = re[k];
        }
    }

    public static void main(String[] args) {
        int[] nums1 = {1};
        int[] nums2 = {};
        for (int i = 0; i < nums1.length; i++) {
            System.out.print(nums1[i] + ",");
        }
        System.out.println();
        for (int i = 0; i < nums2.length; i++) {
            System.out.print(nums2[i] + ",");
        }
        System.out.println();
        merge(nums1, 1, nums2, 0);
        for (int i = 0; i < nums1.length; i++) {
            System.out.print(nums1[i] + ",");
        }
    }
}